﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using _MetalSlug.Soldiers.Units;
using _MetalSlug.Soldiers.Factory;
using System.Xml;
using _MetalSlug.MyCharacters.Characters;

namespace _MetalSlug.Mission
{
    public class MissionBuilder
    {
        protected List<Enemy.TYPE_SOLDIER> lstLoadingEnemy;
        protected int[] countOfEnemy;
        protected int CurrentLoading;

        MyMission buildedMission;
        public MyMission BuildedMission
        {
            get { return buildedMission; }
        }

        MissionFactory CurrentFactory;

        public MissionBuilder(int Level, String NameHero, MissionFactory Factory)
        {
            CurrentLoading = 0;
            CurrentFactory = Factory;
            buildedMission = new MyMission(Level);
            buildedMission.Hero = Factory.CreateCharacter(NameHero);
            buildedMission.Map = Factory.CreateMap(Level, new Vector2(6120, 500));

            lstLoadingEnemy = new List<Enemy.TYPE_SOLDIER> {Enemy.TYPE_SOLDIER.MINIGUN_SOLDIER,
                                                            Enemy.TYPE_SOLDIER.AK_SOLDIER,
                                                            Enemy.TYPE_SOLDIER.BAZOKA_SOLDIER, 
                                                            Enemy.TYPE_SOLDIER.HELICOPER, 
                                                            Enemy.TYPE_SOLDIER.TYCOON};
            countOfEnemy = new int[lstLoadingEnemy.Count];
            InitialNumberOfEnemy(Level);
        }

        public void InitialNumberOfEnemy(int Level)
        {
            XmlDocument Doc = new XmlDocument();
            Doc.Load("Data/Missions.xml");

            XmlNodeList lstEnemy = Doc.SelectSingleNode("//Mission[@id='" + Level + "']").ChildNodes;
            foreach (XmlElement eleEnemy in lstEnemy)
            {
                for (int i = 0; i < lstLoadingEnemy.Count;i++)
                {
                    if(eleEnemy.Name == lstLoadingEnemy[i].ToString())
                    {
                        countOfEnemy[i] = int.Parse(eleEnemy.GetAttribute("Count")) * (GameUtil.CurrentDifficultyLevel + 1);
                        break;
                    }
                }
            }
        }

        public virtual void BuildMission()
        {
            List<Enemy> lstNewEnemy = CurrentFactory.CreateEnemy(lstLoadingEnemy[CurrentLoading],countOfEnemy[CurrentLoading]);
            BuildedMission.LstEnemy.AddRange(lstNewEnemy);
            CurrentLoading++;
        }


        public virtual Boolean isBuildingComplete()
        {
            if (CurrentLoading == lstLoadingEnemy.Count)
            {
                return true;
            }

            return false;
        }

    }
}
